﻿using System.Windows;
#if WPF
using System.Windows.Controls;
using System.Windows.Shapes;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.Foundation;
#endif

namespace Eternal.DataVisualization
{
    public class GridLinesPanel : Canvas
    {
        readonly Axis axis;
        internal Size AvailabelSize;
        public ElementRecycler<Line> GridLinesRecycler { get; set; }
        public GridLinesPanel(Axis axis)
        {
            this.axis = axis;
            GridLinesRecycler = new ElementRecycler<Line>(this);
        }

        internal void PositionGridLines()
        {
            Line gridLine = null;
            if (axis.Labels != null)
            {
                var gridLinesEnum = GridLinesRecycler.RecycleChildren(axis.Labels.Count).GetEnumerator();
                if (axis is XAxis)
                    foreach (var item in axis.Labels)
                    {
                        if (gridLinesEnum.MoveNext())
                            gridLine = gridLinesEnum.Current;
                        if (gridLine == null) continue;
                        gridLine.Style = axis.GridLineStyle;
                        gridLine.X1 = item.Position.X;
                        gridLine.Y2 = ActualHeight;
                        gridLine.X2 = item.Position.X;
                    }
                else
                    foreach (var item in axis.Labels)
                    {
                        if (gridLinesEnum.MoveNext())
                            gridLine = gridLinesEnum.Current;
                        if (gridLine == null) continue;
                        gridLine.Style = axis.GridLineStyle;
                        gridLine.Y1 = item.Position.Y;
                        gridLine.X2 = ActualWidth;
                        gridLine.Y2 = item.Position.Y;
                    }
            }
        }

        //protected override Size MeasureOverride(Size constraint)
        //{
        //    if (availabelSize == constraint || double.IsInfinity(constraint.Height) ||
        //        double.IsInfinity(constraint.Width)) return base.MeasureOverride(constraint);
        //    availabelSize = constraint;
        //    PositionGridLines();
        //    return base.MeasureOverride(constraint);
        //}
    }
}
